$(function(){
	initAjaxForms($('form.ajax-form'));
})
function initAjaxForms(forms) {
	if (! forms) {
		forms = $('form.ajax-form');
	}
	forms.submit(function(){
		var form = $(this);
		var id   = form.attr('id');
		var sbmt = form.find('input[type=submit]');
		if (sbmt.hasClass('loading')) {
			return;
		}
		
		$.ajax({
			url: $(this).attr('action'),
			data: $(this).serialize(),
			type: 'post',
			beforeSend: function() {
				try {
					eval(id + '_before()');
				} catch(e) {
					
				}
				sbmt.addClass('loading');
			},
			complete: function(req) {
				sbmt.removeClass('loading');
				try {
					eval(id + '_after()');
				} catch(e) {
					
				}
				if (req.status == 200) {
					try {
						eval(id + '_complete(req)')
					}catch(e) {
						console.log(e);
						lightbox(req.responseText);
					}
				} else {
					$('#content').html(req.responseText);
					initAjaxForms();
				}
			}
		});
		
		return false;
	});
}